/**
 * 
 */
package com.cipe.cmrs.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cipe.cmrs.model.User;
import com.cipe.cmrs.model.Usertype;
import com.cipe.cmrs.util.DateUtil;
import com.cipe.cmrs.util.Util;

/**
 * @author Incepio
 * 
 */
@Service
@Transactional
public class UsertypeService extends BaseService<Usertype> {

	@Autowired
	private UserService userService;

	public Usertype saveUsertype(Usertype userType, Integer userId) {
		if (userType != null) {
			User user = userService.findById(userId);
			Usertype dbUsertype = findById(userType.getId());
			if (dbUsertype == null) {
				dbUsertype = new Usertype();
				dbUsertype.setCreatedBy(user);
				dbUsertype.setCreationDate(DateUtil.getCurrentDate());
			}
			dbUsertype.setDisplayName(userType.getDisplayName());
			dbUsertype.setUpdatedBy(user);
			dbUsertype.setUpdationDate(DateUtil.getCurrentDate());
			return merge(dbUsertype);
		}
		return null;
	}

	public List<Usertype> findByUserType(String userType) {
		if (Util.isNullOrEmpty(userType)) {
			return null;
		}
		List<Usertype> usertypes = null;
		Object[] params = { userType };
		try {
			usertypes = dao.getNamedQueryResultList("findByUserType", params);
		} catch (Exception ex) {
			logger.warn(ex.toString());
			// TODO: handle exception
		}
		return usertypes;
	}

}
